/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.util.listas;

/**
 *
 * @author Gustavo Canul
 */
public class SimpleList extends List {

    @Override
    public void insertAtFront(Object insertItem) {

        if (isEmpty()) {
            this.firstNode = this.lastNode = new ListNode(insertItem);
        } else {
            this.firstNode = new ListNode(insertItem, this.firstNode);
        }
    }

    @Override
    public void insertAtBack(Object insertItem) {

        if (isEmpty()) {
            this.firstNode = this.lastNode = new ListNode(insertItem);
        } else {
            this.lastNode.setNext(new ListNode(insertItem));
            this.lastNode = this.lastNode.getNext();
        }
    }

    @Override
    public void insertInOrder(Object insertItem) {
        if (isEmpty()) {
            this.firstNode = lastNode = new ListNode(insertItem);
        } else {
            if (firstNode == lastNode) {
                if ((Integer) insertItem > (Integer) firstNode.getData()) {
                    insertAtFront(insertItem);
                } else {
                    insertAtBack(insertItem);
                }
            } else {
                ListNode current = this.firstNode;
                ListNode prev = null;

                while (current != null && (Integer) insertItem < (Integer) current.getData()) {
                    prev = current;
                    current = current.getNext();
                }

                if (prev == null) {
                    insertAtFront(insertItem);
                } else {
                    if (prev == lastNode) {
                        insertAtBack(insertItem);
                    } else {
                        ListNode nuevo = new ListNode(insertItem);
                        prev.setNext(nuevo);
                        nuevo.setNext(current);
                    }
                }
            }
        }
    }

    @Override
    public Object removeFromFront() throws EmptyListException {
        if (isEmpty()) {
            throw new EmptyListException();
        }
        Object removedItem = this.firstNode.getData();
        if (firstNode == lastNode) {
            this.firstNode = this.lastNode = null;
        } else {
            this.firstNode = firstNode.getNext();
        }
        return removedItem;
    }

    @Override
    public Object removeFromBack() throws EmptyListException {
        if (isEmpty()) {
            throw new EmptyListException();
        }

        Object removedItem = lastNode.getData();

        if (firstNode == lastNode) {
            this.firstNode = this.lastNode = null;
        } else {
            ListNode current = this.firstNode;

            while (current.getNext() != lastNode) {
                current = current.getNext();
            }

            current.setNext(null);
        }

        return removedItem;
    }

    @Override
    public Object removeObject(Object elemento) throws EmptyListException {
        if (isEmpty()) {
            throw new EmptyListException();
        }

        if ((Integer) this.firstNode.getData() == (Integer) elemento) {
            removeFromFront();
        }

        ListNode current = this.firstNode;
        ListNode prev = current;

        while (current != null) {

            if ((Integer) current.getData() == (Integer) elemento) {
                prev.setNext(current.getNext());
            } else {
                prev = current;
            }

            current = current.getNext();
        }
        return elemento;
    }
}
